<!DOCTYPE html>
<head>
    <title>socket demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>

<input type="text" id="msg" />
<button id="send">发送</button>

<script>
    var websocket = new WebSocket('ws://127.0.0.1:9502');

    // 打开Socket
    websocket.onopen = function(event)
    {

        $('#send').on('click', function()
        {
            var msg = $('#msg').val();
            websocket.send(msg);
        });
        // 发送一个初始化消息
        websocket.send("客户端初始化！");

        // 监听消息
        websocket.onmessage = function(event) {
            console.log("客户端监听到的消息：", event);
        };

        // 监听Socket的关闭
        websocket.onclose = function(event) {
            console.log("Socket关闭了", event);
        };

        // 关闭Socket....
        // websocket.close()
    };
</script>

</body>
</html>
